﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyOA.BLL;
using MyOA.Model;

namespace MyOA.UI
{
    public partial class _Default : System.Web.UI.Page
    {
        LoginManage manage = new LoginManage();


        /// <summary>
        /// 输入的验证码是否正确
        /// </summary>
        /// <returns>返回输入的验证码是否正确</returns>
        public bool isChecked()
        {
            try
            {
                string code = "";
                code = Session["ValidateCode"].ToString();
                if (code == txtValidate.Text)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                string ee = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns>返回登陆用户实体</returns>
        public UserInfo getLoginUser()
        {
            return new
            UserInfo
            {
                Password = txtLoginPwd.Text,
                LoginId = txtLoginName.Text
            };
        }

        /// <summary>
        /// 构建登陆日志的信息
        /// </summary>
        /// <param name="ifSuccess">是否登陆成功</param>
        /// <returns></returns>
        public LoginLog createLoginLog(string ifSuccess, int userId)
        {
            LoginLog loginLog = new LoginLog();
            loginLog.LoginTime = DateTime.Now;
            loginLog.LoginUserIp = Request.UserHostAddress;
            loginLog.IfSuccess = ifSuccess;
            loginLog.User.UserId = userId;
            return loginLog;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {


            }

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (isChecked())
            {
                //删除验证码信息
                if (Session["ValidateCode"] != null)
                {
                    Session.Remove("ValidateCode");
                }

                UserInfo loginUser = getLoginUser();//获取输入的登陆用户
                if (manage.isLoginSuccessed(loginUser))
                {
                    Session["loginId"] = loginUser.LoginId;//存入登陆用户信息
		    Session.Timeout = 60;
                    LoginLog loginLog = createLoginLog("1", manage.getUserIdByLoginId(txtLoginName.Text));
                    manage.insertLoginLog(loginLog);
                    Response.Redirect("MyOA.aspx");

                }
                else
                {

                    ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('用户或密码出错，请重新填写',null,null,'温馨提示',changeCode);</script>", false);


                }


            }
            else
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "<script>Alert('验证码出错',null,null,'温馨提示',changeCode);</script>", false);
            }
        }

       

    }
}
